package com.maxio.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.maxio.common.enums.ErrorCode;
import com.maxio.domain.dto.DeptDTO.DeptAddDTO;
import com.maxio.domain.dto.DeptDTO.DeptEditDTO;
import com.maxio.domain.dto.PageDTO;
import com.maxio.domain.po.Class;
import com.maxio.domain.po.Dept;
import com.maxio.domain.po.Emp;
import com.maxio.domain.query.DeptQuery;
import com.maxio.domain.vo.ClassVO.IdAndName;
import com.maxio.domain.vo.DeptVO;
import com.maxio.mapper.DeptMapper;
import com.maxio.service.IDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maxio.utils.ThrowUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author enhuan
 * @since 2024-05-28
 */
@Service
@RequiredArgsConstructor
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {
    
    private final DeptMapper deptMapper;

    /**
     * 新增部门信息
     *
     * @param addDTO@return 结果
     */
    @Override
    public Long saveDept(DeptAddDTO addDTO) {
        Dept dept = BeanUtil.copyProperties(addDTO, Dept.class);
        dept.setId((long) RandomUtil.randomInt(100,999999));
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        int result = deptMapper.insert(dept);
        ThrowUtils.throwIf(result <= 0, ErrorCode.OPERATION_ERROR, "新增部门信息失败");
        return dept.getId();
    }

    /**
     * 根据id查询部门信息
     * @param id 部门id
     * @return 结果
     */
    @Override
    public Dept selectById(Long id) {
        ThrowUtils.throwIf(id == null, ErrorCode.PARAMS_ERROR, ErrorCode.PARAMS_ERROR.getMessage());

        Dept dept = deptMapper.selectById(id);
        ThrowUtils.throwIf(dept == null, ErrorCode.NOT_FOUND_ERROR, ErrorCode.NOT_FOUND_ERROR.getMessage());

        return dept;
    }

    /**
     * 修改部门信息
     * @param editDTO 部门信息
     * @return 结果
     */
    @Override
    public Boolean updateDeptById(DeptEditDTO editDTO) {
        Dept dept = BeanUtil.copyProperties(editDTO, Dept.class);
        dept.setUpdateTime(LocalDateTime.now());
        int result = deptMapper.updateById(dept);
        ThrowUtils.throwIf(result <= 0, ErrorCode.OPERATION_ERROR, ErrorCode.OPERATION_ERROR.getMessage());
        return Boolean.TRUE;
    }

    /**
     * 删除部门信息
     * @param id 部门id
     * @return 结果
     */
    @Override
    public Boolean deleteById(Long id) {
        ThrowUtils.throwIf(id == null, ErrorCode.PARAMS_ERROR, ErrorCode.PARAMS_ERROR.getMessage());
        int result = deptMapper.deleteById(id);
        ThrowUtils.throwIf(result <= 0, ErrorCode.OPERATION_ERROR, ErrorCode.OPERATION_ERROR.getMessage());
        return Boolean.TRUE;
    }

    /**
     * 分页查询部门信息
     * @param deptQuery 查询条件
     * @return 结果
     */
    @Override
    public PageDTO<DeptVO> selectPage(DeptQuery deptQuery) {
        String name = deptQuery.getName();

        LambdaQueryWrapper<Dept> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNoneBlank(name), Dept::getName, name);

        Page<Dept> p = new Page<>(deptQuery.getCurrent(), deptQuery.getPageSize());
        IPage<Dept> page = deptMapper.selectPage(p, wrapper);
        return new PageDTO<>(page.getTotal(), page.getPages(), BeanUtil.copyToList(page.getRecords(), DeptVO.class));
    }

    /**
     * 获取部门ID和Name集合
     * @return 结果
     */
    @Override
    public List<IdAndName> selectIdAndName() {

        // 批量查询所有的 Dept 数据
        List<Dept> deptList = deptMapper.selectList(new LambdaQueryWrapper<Dept>()
                .select(Dept::getId, Dept::getName));

        // 将 Emp 数据转换成 IdAndName
        List<IdAndName> nameList = deptList.stream()
                .map(emp -> new IdAndName(emp.getId(), emp.getName()))
                .collect(Collectors.toList());

        return nameList;
    }
}
